package top.z.orm.handle.osql.chain;

import top.z.orm.core.osql.chain.Select;
import top.z.orm.core.osql.chain.UnionSelect;

import static top.z.orm.core.osql.SqlKeyWord.*;

public class UnionSelectImpl extends AbstractSelectToSql implements UnionSelect {

    private UnionSelect useUnionSelect(String keyword, String subSelect1, String subSelect2) {
        sql.add(L_PARENTHESIS).add(subSelect1).add(R_PARENTHESIS).add(keyword);
        sql.add(L_PARENTHESIS).add(subSelect2).add(R_PARENTHESIS);
        return this;
    }

    @Override
    public UnionSelect union(Select subSelect1, Select subSelect2) {
        return union(subSelect1.toSQL(true), subSelect2.toSQL(true));
    }

    @Override
    public UnionSelect union(String subSelect1, String subSelect2) {
        return useUnionSelect(UNION, subSelect1, subSelect2);
    }

    @Override
    public UnionSelect unionAll(Select subSelect1, Select subSelect2) {
        return unionAll(subSelect1.toSQL(true), subSelect2.toSQL(true));
    }

    @Override
    public UnionSelect unionAll(String subSelect1, String subSelect2) {
        return useUnionSelect(UNION_ALL, subSelect1, subSelect2);
    }
}